package com.example.demo.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;

public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    /**
     * 退出登录成功后的代码逻辑
     * @param authentication 要退出登录用户的主体对象
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //销毁会话,清空缓存,重定向
        request.getSession().invalidate();//销毁会话
        authentication.setAuthenticated(false);//设置未登录状态
        response.sendRedirect("/login");//重定向
    }
}
